/**
 * @file FS2711.c
 * @author HYJ
 * @brief
 * @version 0.1
 *
 * @copyright Copyright (c) 2022
 *
 */

#include "FS2711.h"
// #include <stdio.h>
extern void SysTickDelay(uint32_t Delay);
#define DEBUG

// PDO type
#define FS2711_PDO_FIX 0
#define FS2711_PDO_PPS 1

#define FS2711_MAX_5V 1
#define FS2711_MAX_9V 3
#define FS2711_MAX_12V 7
#define FS2711_MAX_20V 15

// Sweep mode
#define FS2711_SWEEP_SAW 0
#define FS2711_SWEEP_TRI 1
#define FS2711_SWEEP_STEP 2

uint8_t gFS2711_Protocol = 0; //<! 当前运行的协议

FS2711_PDO_List_t FS2711_PDO_List = {0};

typedef struct
{
	// SCAN Result
	uint32_t prot_exists;
	uint16_t qc_max_volt;
	uint16_t afc_max_volt;
	uint16_t fcp_max_volt;
	uint16_t scp_max_volt;
	uint16_t scp_min_volt;
	uint16_t scp_max_curr;
	uint16_t scp_min_curr;
	uint8_t vivo_max_power;
} FS2711_Lib_t;

FS2711_Lib_t FS2711_Lib = {0};

#define FS2711_WRITE_ADDR (0x5a) //<! FS2711地址
#define FS2711_READ_ADDR (0x5a)	 //<! FS2711地址

//<! 寄存器列表
#define FS2711_REG_SR 0x4a	   //<! 系统复位寄存器
#define FS2711_REG_MC 0xa0	   //<! 模式设置寄存器
#define FS2711_REG_PR 0x49	   //<! 端口复位寄存器
#define FS2711_REG_DPDM 0x51   //<! DPDM寄存器
#define FS2711_REG_SS 0x40	   //<! 协议扫描寄存器
#define FS2711_REG_STATE1 0xb2 //<! 状态寄存器1

uint16_t FS2711_SCP_Calc(uint8_t val);

/**
 * @brief: FS2711写寄存器数据
 * @author: HYJ
 * @param regAddr: 寄存器地址
 * @param data: 待写入数据
 * @retval None
 */
void FS2711_Write(uint8_t regAddr, uint8_t data)
{
	uint8_t dataBuf[2];
	dataBuf[0] = regAddr;
	dataBuf[1] = data;
	// HAL_I2C_Mem_Write(&hi2c1, FS2711_WRITE_ADDR, regAddr, 1, &dataBuf, 1, HAL_MAX_DELAY);
	I2C_MasterSendDataToSlave(FS2711_WRITE_ADDR, dataBuf, 2);
}

/**
 * @brief: FS2711写寄存器数据
 * @author: HYJ
 * @param regAddr: 寄存器地址
 * @retval None
 */
uint8_t FS2711_Read(uint8_t regAddr)
{
	uint8_t data = 0;
//	uint8_t dataBuf = regAddr;
	// HAL_I2C_Mem_Read(&hi2c1, FS2711_WRITE_ADDR, regAddr, 1, &data, 1, HAL_MAX_DELAY);
	// I2C_MasterSendDataToSlave_noStop(FS2711_WRITE_ADDR, &dataBuf, 1);
	// I2C_MasterRecDataFromSlave(FS2711_READ_ADDR, &data, 1);
	I2C_MasterRecDataFromDevice(FS2711_WRITE_ADDR, regAddr, &data, 1);
	return data;
}

/**
 * @brief 打印PDO列表
 *
 */
void FS2711_Display_PDO_List(void)
{
	uint8_t i = 0;
//	uint8_t buf[40];
	// printf("pdo num %d\r\n", FS2711_PDO_List.pdo_num);
	for (i = 0; i < FS2711_PDO_List.pdo_num; i++)
	{
		// printf("PDO %d:\t%s\tminV: %d\tmaxV: %d\tmaxC: %d\r\n", i,
		//     (FS2711_PDO_List.pdo_type[i] == 1) ? "PPS" : "FIXED",
		//     FS2711_PDO_List.pdo_min_volt[i],
		//     FS2711_PDO_List.pdo_max_volt[i],
		//     FS2711_PDO_List.pdo_max_curr[i]);
		// int ret = sprintf(buf, "%d:%s min:%d\tmax:%d\tC: %d\0", i,
		// 				  (FS2711_PDO_List.pdo_type[i] == 1) ? "P" : "F",
		// 				  FS2711_PDO_List.pdo_min_volt[i],
		// 				  FS2711_PDO_List.pdo_max_volt[i],
		// 				  FS2711_PDO_List.pdo_max_curr[i]);
		// LCD_ShowString(2, i * 12, buf, RED, WHITE, 12, 0);
	}
}
void FS2711_Display_Protocol(void)
{
	uint8_t i = 0;
	// char protocol[24][10] = {"APPLE", "SS2P0", "BC1P2", "TYC", "QC2A", "QC2B", "QC3A", "QC3B", "AFC", "FCP", "SCP", "HISCP", "VOOC", "SVOOC1", "SVOOC2", "VOOC3", "VOOC4", "VIVO", "VIVO2", "VIVO3", "MTK", "PD_PPS", "PD2p0", "PD3p0"};
	while (i++ < 24)
	{
		// if ((FS2711_Lib.prot_exists >> i) & 0x00000001)
		// printf("%s ", protocol[i]);
	}
	// printf("\r\n");
	// printf("QC   MAX V: 0x%x\r\n", FS2711_Lib.qc_max_volt);
	// printf("AFC  MAX V: %d\r\n", FS2711_Lib.afc_max_volt);
	// printf("FCP  MAX V: %d\r\n", FS2711_Lib.fcp_max_volt);
	// printf("SCP  MAX V: %d | MIN V: %d | MAX C: %d | MIN C: %d\r\n", FS2711_Lib.scp_max_volt, FS2711_Lib.scp_min_volt, FS2711_Lib.scp_max_curr, FS2711_Lib.scp_min_curr);
	// printf("VIVO MAX V: %d\r\n", FS2711_Lib.vivo_max_power);
}
void FS2711_Debug(char *file, int ln, char *msg)
{
	// printf("error file: %s\r\nerror line: %d\r\nerror message: %s\r\n", file, ln, msg);
	//  while (1)
	//  	;
}

/**
 * @brief: FS2711选择协议
 * @author: HYJ
 * @param protocol: 选择的协议
 * @retval None
 */
void FS2711_Select_Protocol(uint8_t protocol)
{
	if (protocol > 21)
	{
		FS2711_Debug(__FILE__, __LINE__, "protocol error");
	}
	FS2711_Write(0x42, protocol);
}

/**
 * @brief: FS2711启动协议
 * @author: HYJ
 * @param protocol: 选择的协议
 * @retval None
 */
void FS2711_Enable_Protocol(uint8_t ena)
{
	if (ena)
	{
		FS2711_Write(0x41, 0x1);
	}
	else
	{
		FS2711_Write(0x41, 0x2);
	}
}

/**
 * @brief: FS2711开启电压电流诱骗
 * @author: HYJ
 * @retval None
 */
void FS2711_Enable_Voltage_Current()
{
	FS2711_Write(0x43, 0x1);
}

/**
 * @brief: FS2711获取PD协议信息
 * @author: HYJ
 */
void FS2711_Get_PD_Information(void)
{
	uint8_t i = 0;
	uint8_t pdo_b0 = 0, pdo_b1 = 0, pdo_b2 = 0, pdo_b3 = 0;

	//<! 查询PD信息之前会先退出当前的协议，因为查询PD信息需要进入PD模式
	FS2711_Enable_Protocol(0); //<! 退出当前协议
	SysTickDelay(100);
	FS2711_Select_Protocol(FS2711_PROTOCOL_PD); //<! 进入PD协议
	FS2711_Enable_Protocol(1);

	FS2711_PDO_List.pdo_num = 0;
	memset(FS2711_PDO_List.pdo_type, 0, 7);
	memset(FS2711_PDO_List.pdo_max_volt, 0, 7);
	memset(FS2711_PDO_List.pdo_min_volt, 0, 7);
	memset(FS2711_PDO_List.pdo_max_curr, 0, 7);

	SysTickDelay(2000);//2000
	for (i = 0; i < 7; i++)
	{
		pdo_b0 = FS2711_Read(0xc0 + i * 4);
		pdo_b1 = FS2711_Read(0xc1 + i * 4);
		pdo_b2 = FS2711_Read(0xc2 + i * 4);
		pdo_b3 = FS2711_Read(0xc3 + i * 4);

		FS2711_Write(0xc0 + i * 4, 0);
		FS2711_Write(0xc1 + i * 4, 0);
		FS2711_Write(0xc2 + i * 4, 0);
		FS2711_Write(0xc3 + i * 4, 0);
		if (pdo_b0)
		{
			if (pdo_b3 & 0xc0)
			{
				FS2711_PDO_List.pdo_type[i] = 1;
				FS2711_PDO_List.pdo_min_volt[i] = pdo_b1 * 100;
				FS2711_PDO_List.pdo_max_volt[i] = ((pdo_b2 >> 1) + ((pdo_b3 & 0x1) << 7)) * 100;
				FS2711_PDO_List.pdo_max_curr[i] = (pdo_b0 & 0x7f) * 50;
			}
			else
			{
				FS2711_PDO_List.pdo_min_volt[i] = ((pdo_b1 >> 2) + ((pdo_b2 & 0xf) << 6)) * 50;
				FS2711_PDO_List.pdo_max_volt[i] = FS2711_PDO_List.pdo_min_volt[i];
				FS2711_PDO_List.pdo_max_curr[i] = (pdo_b0 + ((pdo_b1 & 0x3) << 8)) * 10;
			}
			FS2711_PDO_List.pdo_num++;
		}
		else
		{
			continue;
		}
	}
	FS2711_Enable_Protocol(0);
	SysTickDelay(500);

#ifdef DEBUG
	FS2711_Display_PDO_List();
#endif
}

/**
 * @brief
 *
 * @param PDOID PDO从0开始 最高为6
 */
void FS2711_Open_PD(uint8_t PDOID)
{

	if (PDOID >= FS2711_PDO_List.pdo_num) //<! 不能超过已有PDO数量
	{
#ifdef DEBUG
		FS2711_Debug(__FILE__, __LINE__, "PDOID error");
#endif
		return;
	}
	if (FS2711_PDO_List.pdo_type[PDOID] != 0)
	{
#ifdef DEBUG
		FS2711_Debug(__FILE__, __LINE__, "PDOID error");
#endif
		return;
	}
	if (gFS2711_Protocol != FS2711_PROTOCOL_PD)
	{
		FS2711_Enable_Protocol(0);
		SysTickDelay(500);

		FS2711_Select_Protocol(FS2711_PROTOCOL_PD);
		FS2711_Enable_Protocol(1);
		SysTickDelay(2000);//3000
	}

	FS2711_Write(0x46, PDOID + (PDOID << 4));

	FS2711_Enable_Voltage_Current();
	gFS2711_Protocol = FS2711_PROTOCOL_PD;
}

/**
 * @brief
 *
 * @param PDOID
 * @param start_volt 起始电压
 * @param end_volt 结束电压
 * @param max_curr 最大电流
 */
void FS2711_Open_PPS(uint8_t PDOID, uint16_t start_volt, uint16_t end_volt, uint16_t max_curr)
{
	uint16_t wr_l, wr_h;
	if (PDOID >= FS2711_PDO_List.pdo_num) //<! 不能超过已有PDO数量
	{
#ifdef DEBUG
		FS2711_Debug(__FILE__, __LINE__, "PDOID error");
#endif
		return;
	}
	if ((start_volt > FS2711_PDO_List.pdo_max_volt[PDOID]) || (start_volt < FS2711_PDO_List.pdo_min_volt[PDOID]))
	{
#ifdef DEBUG
		FS2711_Debug(__FILE__, __LINE__, "start_volt error");
#endif
		return;
	}
	if ((end_volt > FS2711_PDO_List.pdo_max_volt[PDOID]) || (start_volt < FS2711_PDO_List.pdo_min_volt[PDOID]))
	{
#ifdef DEBUG
		FS2711_Debug(__FILE__, __LINE__, "end_volt error");
#endif
		return;
	}
	if (max_curr > FS2711_PDO_List.pdo_max_curr[PDOID])
	{
#ifdef DEBUG
		FS2711_Debug(__FILE__, __LINE__, "max_curr error");
#endif
		return;
	}
	if (FS2711_PDO_List.pdo_type[PDOID] != 1)
	{
#ifdef DEBUG
		FS2711_Debug(__FILE__, __LINE__, "pdo_type error");
#endif
		return;
	}

	if (gFS2711_Protocol != FS2711_PROTOCOL_PPS)
	{
		FS2711_Enable_Protocol(0);
		SysTickDelay(500);

		FS2711_Select_Protocol(FS2711_PROTOCOL_PD);
		FS2711_Enable_Protocol(1);
		SysTickDelay(100);

		FS2711_Select_Protocol(FS2711_PROTOCOL_PPS);
		FS2711_Enable_Protocol(1);
		SysTickDelay(3000);
	}

	FS2711_Write(0x46, PDOID + (PDOID << 4));

	wr_l = (start_volt - FS2711_PDO_List.pdo_min_volt[PDOID]) / 20;
	wr_h = (end_volt - FS2711_PDO_List.pdo_min_volt[PDOID]) / 20;

	//	wr_l = (start_volt - 3300) / 20;
	//	wr_h = (end_volt - 3300) / 20;

	FS2711_Write(0xde, max_curr / 50);

	FS2711_Write(0xf4, wr_l & 0xff);
	FS2711_Write(0xf5, (wr_l >> 8) & 0xff);
	FS2711_Write(0xf6, wr_h & 0xff);
	FS2711_Write(0xf7, (wr_h >> 8) & 0xff);

	FS2711_Enable_Voltage_Current();

	gFS2711_Protocol = FS2711_PROTOCOL_PPS;
}
/**
 * @brief QC2.0支持5V/9V/12V固定电压，最高24W，最大电流2A
 *
 * @param volt 诱骗电压
 */
void FS2711_Open_QC2(uint16_t volt)
{

	uint16_t wr_l, wr_h;
	if (gFS2711_Protocol != FS2711_PROTOCOL_QC2)
	{
		FS2711_Enable_Protocol(0);
		SysTickDelay(500);

		FS2711_Select_Protocol(FS2711_PROTOCOL_QC2);
		FS2711_Enable_Protocol(1);
		SysTickDelay(3000);
	}

	switch (volt)
	{
	case 5000:
		wr_l = 0x0;
		wr_h = 0x0;
		break;
	case 9000:
		wr_l = 0x1;
		wr_h = 0x1;
		break;
	case 12000:
		wr_l = 0x2;
		wr_h = 0x2;
		break;
	case 20000:
		wr_l = 0x3;
		wr_h = 0x3;
		break;
	default:
		FS2711_Debug(__FILE__, __LINE__, "Voltage error");
		break;
	}

	FS2711_Write(0xf4, wr_l & 0xff);
	FS2711_Write(0xf5, (wr_l >> 8) & 0xff);
	FS2711_Write(0xf6, wr_h & 0xff);
	FS2711_Write(0xf7, (wr_h >> 8) & 0xff);

	FS2711_Enable_Voltage_Current();
	gFS2711_Protocol = FS2711_PROTOCOL_QC2;
}
/**
 * @brief AFC支持电压档位有 5V、9V、12V，最大输出功率为 18W（9V/2A）
 *
 * @param volt 诱骗电压
 */
void FS2711_Open_AFC(uint16_t volt)
{

	uint16_t wr_l, wr_h;
	if (gFS2711_Protocol != FS2711_PROTOCOL_AFC)
	{
		FS2711_Enable_Protocol(0);
		SysTickDelay(500);

		FS2711_Select_Protocol(FS2711_PROTOCOL_AFC);
		FS2711_Enable_Protocol(1);
		SysTickDelay(3000);
	}

	switch (volt)
	{
	case 5000:
		wr_l = 0x0;
		wr_h = 0x0;
		break;
	case 9000:
		wr_l = 0x1;
		wr_h = 0x1;
		break;
	case 12000:
		wr_l = 0x2;
		wr_h = 0x2;
		break;
	case 20000:
		wr_l = 0x3;
		wr_h = 0x3;
		break;
	default:
		FS2711_Debug(__FILE__, __LINE__, "Voltage error");
		break;
	}

	FS2711_Write(0xf4, wr_l & 0xff);
	FS2711_Write(0xf5, (wr_l >> 8) & 0xff);
	FS2711_Write(0xf6, wr_h & 0xff);
	FS2711_Write(0xf7, (wr_h >> 8) & 0xff);

	FS2711_Enable_Voltage_Current();
	gFS2711_Protocol = FS2711_PROTOCOL_AFC;
}
/**
 * @brief 采用9V/2A输出，最高功率18W
 *
 * @param volt 诱骗电压
 */
void FS2711_Open_FCP(uint16_t volt)
{

	uint16_t wr_l, wr_h;
	if (gFS2711_Protocol != FS2711_PROTOCOL_FCP)
	{
		FS2711_Enable_Protocol(0);
		SysTickDelay(500);

		FS2711_Select_Protocol(FS2711_PROTOCOL_FCP);
		FS2711_Enable_Protocol(1);
		SysTickDelay(3000);
	}

	switch (volt)
	{
	case 5000:
		wr_l = 0x0;
		wr_h = 0x0;
		break;
	case 9000:
		wr_l = 0x1;
		wr_h = 0x1;
		break;
	case 12000:
		wr_l = 0x2;
		wr_h = 0x2;
		break;
	case 20000:
		wr_l = 0x3;
		wr_h = 0x3;
		break;
	default:
		FS2711_Debug(__FILE__, __LINE__, "Voltage error");
		break;
	}

	FS2711_Write(0xf4, wr_l & 0xff);
	FS2711_Write(0xf5, (wr_l >> 8) & 0xff);
	FS2711_Write(0xf6, wr_h & 0xff);
	FS2711_Write(0xf7, (wr_h >> 8) & 0xff);

	FS2711_Enable_Voltage_Current();
	gFS2711_Protocol = FS2711_PROTOCOL_FCP;
}
/**
 * @brief QC3.0支持3.6V~12V可变电压（0.2V为单位），最高36W，最大电流3A
 *
 * @param volt 诱骗电压
 */
void FS2711_Open_QC3(uint16_t volt)
{

	uint16_t wr_l, wr_h;
	if (gFS2711_Protocol != FS2711_PROTOCOL_QC3)
	{
		FS2711_Enable_Protocol(0);
		SysTickDelay(500);

		FS2711_Select_Protocol(FS2711_PROTOCOL_QC3);
		FS2711_Enable_Protocol(1);
		SysTickDelay(3000);
	}

	wr_l = (volt - 3600) / 200;
	wr_h = (volt - 3600) / 200;

	FS2711_Write(0xf4, wr_l & 0xff);
	FS2711_Write(0xf5, (wr_l >> 8) & 0xff);
	FS2711_Write(0xf6, wr_h & 0xff);
	FS2711_Write(0xf7, (wr_h >> 8) & 0xff);

	FS2711_Enable_Voltage_Current();
	gFS2711_Protocol = FS2711_PROTOCOL_QC3;
}
/**
 * @brief 3.3V+10mV*N
 *
 * @param volt 诱骗电压
 */
void FS2711_Open_SCP(uint16_t volt)
{

	uint16_t wr_l, wr_h;
	if (gFS2711_Protocol != FS2711_PROTOCOL_SCP)
	{
		FS2711_Enable_Protocol(0);
		SysTickDelay(500);

		FS2711_Select_Protocol(FS2711_PROTOCOL_SCP);
		FS2711_Enable_Protocol(1);
		SysTickDelay(3000);
	}

	wr_l = (volt - 3300) / 10;
	wr_h = (volt - 3300) / 10;

	FS2711_Write(0xf4, wr_l & 0xff);
	FS2711_Write(0xf5, (wr_l >> 8) & 0xff);
	FS2711_Write(0xf6, wr_h & 0xff);
	FS2711_Write(0xf7, (wr_h >> 8) & 0xff);

	FS2711_Enable_Voltage_Current();
	gFS2711_Protocol = FS2711_PROTOCOL_SCP;
}
/**
 * @brief 5.5V+10mV*N, max 11V
 *
 * @param volt 诱骗电压
 */
void FS2711_Open_HISCP(uint16_t volt)
{

	uint16_t wr_l, wr_h;
	if (gFS2711_Protocol != FS2711_PROTOCOL_HISCP)
	{
		FS2711_Enable_Protocol(0);
		SysTickDelay(500);

		FS2711_Select_Protocol(FS2711_PROTOCOL_HISCP);
		FS2711_Enable_Protocol(1);
		SysTickDelay(3000);
	}

	wr_l = (volt - 5500) / 10;
	wr_h = (volt - 5500) / 10;

	FS2711_Write(0xf4, wr_l & 0xff);
	FS2711_Write(0xf5, (wr_l >> 8) & 0xff);
	FS2711_Write(0xf6, wr_h & 0xff);
	FS2711_Write(0xf7, (wr_h >> 8) & 0xff);

	FS2711_Enable_Voltage_Current();
	gFS2711_Protocol = FS2711_PROTOCOL_HISCP;
}
uint8_t g_test_pd_up = 0;
/**
 * @brief: FS2711获取扫描的协议
 * @author: HYJ
 * @retval None
 */
void FS2711_Get_State(void)
{
	uint8_t data = 0, i = 0;
	uint8_t pdo_b0 = 0, pdo_b1 = 0, pdo_b2 = 0, pdo_b3 = 0;

	typedef struct
	{
		uint8_t scan : 1;
		uint8_t pd_up : 1;
		uint8_t : 6;
	} state1_reg;

	state1_reg *sr;

	data = FS2711_Read(0xb1);
	data = FS2711_Read(FS2711_REG_STATE1);

	sr = (state1_reg *)&data;
	g_test_pd_up = sr->pd_up;
	if (sr->pd_up == 0) //<! PD有更新
	{

		FS2711_PDO_List.pdo_num = 0;
		memset(FS2711_PDO_List.pdo_type, 0, 7);
		memset(FS2711_PDO_List.pdo_max_volt, 0, 7);
		memset(FS2711_PDO_List.pdo_min_volt, 0, 7);
		memset(FS2711_PDO_List.pdo_max_curr, 0, 7);

		for (i = 0; i < 7; i++) // PDO信息存放在寄存器中 故读取7次
		{
			pdo_b0 = FS2711_Read(0xc0 + i * 4);
			pdo_b1 = FS2711_Read(0xc1 + i * 4);
			pdo_b2 = FS2711_Read(0xc2 + i * 4);
			pdo_b3 = FS2711_Read(0xc3 + i * 4); // 一共32位数据

			FS2711_Write(0xc0 + i * 4,0);
			FS2711_Write(0xc1 + i * 4,0);
			FS2711_Write(0xc2 + i * 4,0);
			FS2711_Write(0xc3 + i * 4,0);
			if (pdo_b0)
			{
				if (pdo_b3 & 0xc0)
				{
					FS2711_PDO_List.pdo_type[i] = 1;
					FS2711_PDO_List.pdo_min_volt[i] = pdo_b1 * 100;
					FS2711_PDO_List.pdo_max_volt[i] = ((pdo_b2 >> 1) + ((pdo_b3 & 0x1) << 7)) * 100;
					FS2711_PDO_List.pdo_max_curr[i] = (pdo_b0 & 0x7f) * 50;
				}
				else
				{
					FS2711_PDO_List.pdo_min_volt[i] = ((pdo_b1 >> 2) + ((pdo_b2 & 0xf) << 6)) * 50;
					FS2711_PDO_List.pdo_max_volt[i] = FS2711_PDO_List.pdo_min_volt[i];
					FS2711_PDO_List.pdo_max_curr[i] = (pdo_b0 + ((pdo_b1 & 0x3) << 8)) * 10;
				}
				FS2711_PDO_List.pdo_num++;
			}
			else
			{
				continue;
			}
		}
		// printf("OPEN PD:%d max_volt:%d max_curr:%d\r\n",i-1,FS2711_PDO_List.pdo_max_volt[i-1],FS2711_PDO_List.pdo_max_curr[i-1]);
	}
}

/**
 * @brief: FS2711开始扫描协议
 * @author: HYJ
 */
void FS2711_Scan_Start(void)
{
	FS2711_Write(FS2711_REG_SS, 0x1);
}

/**
 * @brief: FS2711 复位DP & DM
 */
void FS2711_DPDM_Reset(void)
{
	FS2711_Write(FS2711_REG_DPDM, 0x00); //<! 拉低DP & DM
}

/**
 * @brief: FS2711系统复位
 * @author: HYJ
 */
void FS2711_System_Reset(void)
{
	FS2711_Write(FS2711_REG_SR, 0x1);
}

/**
 * @brief: FS2711端口复位
 * @author: HYJ
 */
void FS2711_Port_Reset(void)
{
	FS2711_Write(FS2711_REG_MC, 0x02); //<! 断开Rd
	FS2711_Write(FS2711_REG_PR, 0x00); //<! 端口复位
	SysTickDelay(100);
	FS2711_Write(FS2711_REG_MC, 0x00);
	FS2711_Write(FS2711_REG_PR, 0x01);
}

uint16_t FS2711_SCP_Calc(uint8_t val) //<! SCP值计算
{
	uint16_t result;
	if ((val & 0xc0) == 0xc0)
	{
		result = (val & 0x3f) * 1000;
	}
	else if ((val & 0xc0) == 0x80)
	{
		result = (val & 0x3f) * 100;
	}
	else if ((val & 0xc0) == 0x40)
	{
		result = (val & 0x3f) * 10;
	}
	return result;
}
void FS2711_Init(void)
{
	__SYSCTRL_GPIOA_CLK_ENABLE();
	__SYSCTRL_I2C_CLK_ENABLE();

	GPIO_InitTypeDef GPIO_InitStructure = {0};

	PA10_AFx_I2CSDA();
	PA09_AFx_I2CSCL();
	GPIO_InitStructure.Pins = GPIO_PIN_9 | GPIO_PIN_10;
	GPIO_InitStructure.Mode = GPIO_MODE_OUTPUT_OD;
	GPIO_Init(CW_GPIOA, &GPIO_InitStructure);

	I2C_InitTypeDef I2C_InitStruct = {0};

	// I2C初始化
	I2C_InitStruct.PCLK_Freq = 48000000;
	I2C_InitStruct.I2C_SCL_Freq = 400000;
	I2C_InitStruct.I2C_SCL_Source = I2C_SCL_SRC_GPIO;
	I2C_InitStruct.I2C_SDA_Source = I2C_SDA_SRC_GPIO;

	I2C_DeInit();
	I2C_Master_Init(&I2C_InitStruct); // 初始化模块
	I2C_AcknowledgeConfig(ENABLE);	  // ACK打开
	I2C_Cmd(ENABLE);				  // 模块使能

}
uint32_t g_prot_exist;
void FS2711_prot_exist(void)
{
//	uint8_t pe_b0 = 0, pe_b1 = 0, pe_b2 = 0, pe_b3 = 0;
//	FS2711_Scan_Start();
//	SysTickDelay(500);
//	pe_b0 =  FS2711_Read(0xf8);
//	pe_b1 =  FS2711_Read(0xf9);
//	pe_b2 =  FS2711_Read(0xfa);
//	pe_b3 =  FS2711_Read(0xfb);
//	g_prot_exist = pe_b1 << 16;
//	g_prot_exist |= pe_b2 << 8;
//	g_prot_exist |= pe_b3;
}
uint8_t cmp_volt(uint16_t set_val,uint16_t err_val)
{
//	uint16_t vbus = get_vbus_value();
//	if(vbus > (set_val - err_val) && vbus < (set_val+err_val))
//	{
//		return 1;
//	}
	return 0;
}
uint16_t QC2_Volt_flag;
uint16_t QC2_Volt_data[3] = {5000,9000,12000};
void FS2711_QC2_scan(void)
{
	for (int i = 0; i < sizeof(QC2_Volt_data) / 2; i++)
	{
		FS2711_Open_QC2(QC2_Volt_data[i]); // 5V/9V/12V
		SysTickDelay(500);
		QC2_Volt_flag &= ~(0x01 << i);//先清0
		uint8_t ret = cmp_volt(QC2_Volt_data[i],500);
		QC2_Volt_flag |= ret << i;//如果是1就置1，是0就不变
	}
}
uint16_t get_QC2_volt_flag(void)
{
	return QC2_Volt_flag;
}
uint16_t QC3_Volt_flag;
uint16_t QC3_Volt_data[2] = {3600,12000};
void FS2711_QC3_scan(void)
{
	for (int i = 0; i < sizeof(QC3_Volt_data) / 2; i++)
	{
		FS2711_Open_QC3(QC3_Volt_data[i]); // 5V/9V/12V
		SysTickDelay(500);
		QC3_Volt_flag &= ~(0x01 << i);//先清0
		uint8_t ret = cmp_volt(QC3_Volt_data[i],500);
		QC3_Volt_flag |= ret << i;//如果是1就置1，是0就不变
	}
}
uint16_t get_QC3_volt_flag(void)
{
	return QC3_Volt_flag;
}
void FS2711_other_protocol(void)
{
	// 下面注释每次打开一个测试
	FS2711_QC2_scan();
	FS2711_QC3_scan();
	// FS2711_Open_AFC(9000);	 // 5V、9V、12V
	// FS2711_Open_FCP(9000);	 // 5V、9V、12V、20V
	
	// FS2711_Open_QC3(3600);	 // 3.6V~12V+200mv
	// FS2711_Open_SCP(3300);	 // 3.3v+10mv
	// FS2711_Open_HISCP(5500); // 5.5+10mv
}
/**
 * @brief 
 * 
 */
void FS2711_Deinit(void)
{
	gFS2711_Protocol = 0;
	memset(&FS2711_PDO_List,0x00,sizeof(FS2711_PDO_List_t));//复位数据
}
